package org.example.config

import ai.platon.pulsar.crawl.CrawlLoops
import ai.platon.pulsar.dom.FeatureCalculatorFactory
import ai.platon.pulsar.dom.features.ChainedFeatureCalculator
import ai.platon.scent.ScentEnvironment
import org.example.core.calculator.AmazonFeatureCalculator
import org.slf4j.LoggerFactory
import org.springframework.context.ApplicationContextInitializer
import org.springframework.context.support.AbstractApplicationContext

class CrawlerInitializer : ApplicationContextInitializer<AbstractApplicationContext> {

    private val logger = LoggerFactory.getLogger(CrawlerInitializer::class.java)


    override fun initialize(applicationContext: AbstractApplicationContext) {
        ScentEnvironment().checkEnvironment()

        logger.info("Initializing feature calculator, append AmazonFeatureCalculator")

        val calculator = FeatureCalculatorFactory.calculator as? ChainedFeatureCalculator
        calculator?.calculators?.add(AmazonFeatureCalculator())
        // ignore the default crawl loop, ScentCrawlLoop is expected
        CrawlLoops.filters.add { loop -> loop.name != "DefaultCrawlLoop" }
    }

}